{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lists and Loops [demonstration]\n",
    "The code below shows some ways of using python **lists** (known as **arrays** in many languages).  Note how these lists are \"sliced\" from index i to j using `my_list[i:j]` notation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my_list is: [1, 2, 3, 'a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "my_list = [1, 2, 3, \"a\", \"b\", \"c\"]\n",
    "print(\"my_list is:\", my_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looping through a list...\n",
      "item is 1\n",
      "item is 2\n",
      "item is 3\n",
      "item is a\n",
      "item is b\n",
      "item is c\n"
     ]
    }
   ],
   "source": [
    "print(\"Looping through a list...\")\n",
    "for item in my_list:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The len function is important!\n",
      "my_list has 6 elements\n"
     ]
    }
   ],
   "source": [
    "print(\"The len function is important!\")\n",
    "num_elements = len(my_list)\n",
    "print(\"my_list has\", num_elements, \"elements\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A less great way to loop through a list...\n",
      "item is 1\n",
      "item is 2\n",
      "item is 3\n",
      "item is a\n",
      "item is b\n",
      "item is c\n"
     ]
    }
   ],
   "source": [
    "print(\"A less great way to loop through a list...\")\n",
    "for index in range(len(my_list)):\n",
    "    item = my_list[index] # accessing an element in a list!\n",
    "    print(\"item is\", item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slicing a list from beginning...\n",
      "item is 1\n",
      "item is 2\n",
      "item is 3\n"
     ]
    }
   ],
   "source": [
    "print(\"Slicing a list from beginning...\")\n",
    "for item in my_list[:3]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slicing a list to the end...\n",
      "item is a\n",
      "item is b\n",
      "item is c\n"
     ]
    }
   ],
   "source": [
    "print(\"Slicing a list to the end...\")\n",
    "for item in my_list[3:]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Slicing a list in the middle...\n",
      "item is 3\n",
      "item is a\n"
     ]
    }
   ],
   "source": [
    "print(\"Slicing a list in the middle...\")\n",
    "for item in my_list[2:4]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enumerating a list...\n",
      "item number 0 is 1\n",
      "item number 1 is 2\n",
      "item number 2 is 3\n",
      "item number 3 is a\n",
      "item number 4 is b\n",
      "item number 5 is c\n"
     ]
    }
   ],
   "source": [
    "print(\"Enumerating a list...\")\n",
    "for i, item in enumerate(my_list):\n",
    "    print(\"item number\", i, \"is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Another way to enumerate using a list 'method'...\n",
      "item 1 has index 0\n",
      "item 2 has index 1\n",
      "item 3 has index 2\n",
      "item a has index 3\n",
      "item b has index 4\n",
      "item c has index 5\n"
     ]
    }
   ],
   "source": [
    "print(\"Another way to enumerate using a list 'method'...\")\n",
    "for item in my_list:\n",
    "    index = my_list.index(item)\n",
    "    print(\"item\", item, \"has index\", index)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
